package wowmobileclient;

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Alert;
import javax.microedition.midlet.MIDlet;

public class Client extends MIDlet implements CommandListener {

    private Command exitCommand;
    private Display mainDisplay;
    private Canvas gameCanvas;
    private boolean stopSender = false;
    private Communication c;
    private Form LoginFrm;
    private TextField LoginTxt;
    private Command okCmd;
    private String login;
    String area;

    public Client() {
        exitCommand = new Command("Exit", Command.SCREEN, 0);
        mainDisplay = Display.getDisplay(this);

        StreamConnection channel;
        try {
            channel = (StreamConnection) Connector.open("socket://127.0.0.1:3456");
        } catch (Exception e) {
            System.err.print("Error in Client creating socket:" + e);
            return;
        }
        c = new Communication(channel);
    }

    public Communication getC() {
        return c;
    }

    public void startApp() {
        LoginTxt = new TextField("Login:", "", 50, TextField.ANY);
        okCmd = new Command("OK", Command.OK, 0);
        LoginFrm = new Form("User Login", new Item[] {LoginTxt});
        LoginFrm.addCommand(okCmd);
        LoginFrm.setCommandListener(this);
        LoginTxt.setString("dziadek");
        mainDisplay.setCurrent(LoginFrm);      
    }
    public void makeCanvas(){
        try {
            gameCanvas = new Canvas(this);
            gameCanvas.start();
            gameCanvas.addCommand(exitCommand);
            gameCanvas.setCommandListener(this);
        } catch (IOException ioexception) {
            System.err.println("Problem loading image " + ioexception);
        }
        mainDisplay.setCurrent(gameCanvas);

        //przy pomocy tej metody mozemy aktualizowac graczy na planszy
        //kazdy jest odzdielony tym separatorem - [:]
        //atrybuty sa oddzielone przecinkami: typ pola (numer grafiki z resource), x, y
        //gameCanvas.users.addFromString("1,9,2[:]1,10,2");
    }
    public void validateUser(String login) {
        c.sendString(login);
        try{
        if (c.getIsLogin()) {
            makeCanvas();
            this.login=login;
        } else {
            tryAgain();
        }
        } catch (Exception e) {
            System.err.println("Error in checking login:" + e);
        }
    }
    
    public void tryAgain() {
        Alert error = new Alert("Login Incorrect, please try again");
        mainDisplay.setCurrent(error);
  }
    //tymczasowy
    public void sender(Action a) {
        try {
            c.sendAction(a);


        } catch (Exception e) {
            System.err.println("Error in close:" + e);
        }
    }
    
    public void restartApp(int[][] items) {
        gameCanvas.restart(items);
        startApp();
    }

    public void nextLevel() {
        gameCanvas.stop();
        makeCanvas();
    }

    protected void destroyApp(boolean unconditional) {
        stopSender = true;
        gameCanvas.stop();
    }

    protected void pauseApp() {
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == okCmd) {
               validateUser(LoginTxt.getString());        
        }
        if (command == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
        }

        if(command == gameCanvas.getOkCmd())
        {
            gameCanvas.invitePlayer();
        }
    }

    public Display getMainDisplay() {
        return mainDisplay;
    }

    public String getLogin() {
        return login;
    }



}